package com.qyhl.module_practice.activity.detail.apply;

import android.annotation.SuppressLint;

import com.google.gson.Gson;
import com.qyhl.module_practice.common.PracticeUrl;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.UpTokenBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeAcitivityBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class PracticeActApplyModel implements PracticeActApplyContract.PracticeActApplyModel {
    private PracticeActApplyPresenter mPresenter;

    PracticeActApplyModel(PracticeActApplyPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }


    @Override
    public void getActList(String volId) {

        EasyHttp.get(PracticeUrl.GET_PRACTICE_ACT_NOT_CHECK_LIST)
                .params("volId", volId)
                .execute(new SimpleCallBack<List<PracticeAcitivityBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.actListError("暂无未上传资料申请积分的志愿者服务项目！");
                        } else {
                            mPresenter.actListError("获取出错，请稍后再试！");
                        }
                    }

                    @Override
                    public void onSuccess(List<PracticeAcitivityBean> practiceAcitivityBeans) {
                        if (practiceAcitivityBeans != null && practiceAcitivityBeans.size() > 0) {
                            mPresenter.setActList(practiceAcitivityBeans);
                        } else {
                            mPresenter.actListError("暂无未上传资料申请积分的志愿者服务项目！");
                        }
                    }
                });

    }

    @Override
    public void applyScore(String volId, String title, String desc, String actId, String creator, String imgs) {

        EasyHttp.post(PracticeUrl.POST_PRACTICE_APPLY_SCORE)
                .params("siteId", CommonUtils.getInstance().getSite()+"")
                .params("volId", volId)
                .params("title", title)
                .params("desc", desc)
                .params("actId", actId)
                .params("creator", creator)
                .params("imgs", imgs)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.applyError("提交失败，请稍后再试！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult response  = new Gson().fromJson(s,ApiResult.class);

                        if (response.getCode() == 200) {
                            mPresenter.applySuccess("提交成功，请耐心等待积分审核发放！");
                        } else {
                            mPresenter.applyError("提交出错，请稍后再试！");
                        }
                    }
                });

    }

    @Override
    public void getToken(final boolean isUpload) {



        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CommonUtils.getInstance().getCircleUrl() + "?timestamp=" + time;

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=getUpToken&type=3"))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<UpTokenBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getTokenError(isUpload);
                    }

                    @Override
                    public void onSuccess(UpTokenBean upTokenBean) {
                        mPresenter.getTokenSuccess(upTokenBean, isUpload);
                    }
                });

    }
}
